www.gusucode.com > XerCMS 携云PHP企业建站程序 v2015PHP源码程序 > XerCMS 携云PHP企业建站程序 v2015/XerCMS_v20150724/XerCMS_v20150724/XerCMS/Library/XerCMS_http.php

    <?php
/**
 * @name     XERCMS
 * @author   Arno <XerCMS@163.com> [QQ:1328013]
 * @version  1.0.0
 * @link     http://www.XerCMS.com 
 */

!defined('XERCMS') && exit('Access Denied');

class Http
{
	var $result;
	var $url;
	var $error;
	var $errno;
	var $referer;
	var $domain; 
	var $ch;
  
	function request($url) {
		$this->result = '';
     	if(preg_match('/(http|https)\:\/\/([^\/]+)/i',$url,$match)) {
          	$this->referer = $match[0];
          	$this->domain = $match[2];
     	} else {
          	$this->result = NULL;
          	return;
     	}
     	//exit($this->referer);
    		if(function_exists('curl_init')) {
          	$this->ch = curl_init();
      		$this->curl($url);
		} else if(function_exists('fsockopen')) {
	  		$this->open($url);	
		} else {
	   		$this->result = file_get_contents($url);
		}
  	}
  
	function open($url) {
    		$ret = '';$url = explode('/',$url,4);//print_r($url);
    		$http = fsockopen($url[2],80,$e,$n,10);
		if(!$http) {
	  		$this->result  = '';
		} else {
      		$put = "GET /".$url[3]." HTTP/1.1\r\n";
      		$put .= "Host: ".$url[2]."\r\n";
      		$put .= "Connection: Close\r\n\r\n";
      		fwrite($http, $put);
      		while (!feof($http)) {
        			$this->result .= fgets($http, 128);
      		}
      		fclose($http);
	  		if(strpos($this->result,"\r\n\r\n") !== false) {
	    			$this->result = explode("\r\n\r\n",$this->result,2);
	    			$this->result = $this->result[1];
	  		}//print_R($this->result);exit;
		} 
		return;
  	}
  
  	function curl($url,$count = 3) {    
		//$this->ch = curl_init();
          $this->error ='2222';
	  	if($this->ch) {
            	$this->result = '';
	      	curl_setopt($this->ch, CURLOPT_URL,$url);
               curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	       	curl_setopt($this->ch, CURLOPT_HEADER, 0);
            	curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 SE 2.X MetaSr 1.0');
	       	curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 3);
	       	curl_setopt($this->ch, CURLOPT_TIMEOUT, 18);
            	curl_setopt($this->ch, CURLOPT_FRESH_CONNECT, 0);
            	curl_setopt($this->ch, CURLOPT_REFERER,$this->referer);
	       	curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
               
           	$this->result  = curl_exec($this->ch);
	       	if($this->result === FALSE) {
				// $this->error = curl_error($this->ch);
                 	//echo $this->error;
				if($count > 0) {
				  	sleep(1);
                      	$count--;
				  	$this->curl($url,$count);
                      	return;
				} else {
                         $this->result = null;
                         $this->error = curl_error($this->ch);
                         return;
                    }
	       	}
            	//curl_close($this->ch);
	  	} else {
            	//echo '11111111111222222222221';
             	$this->result = null;
             	return;
       	}
	  
  	}
  
     function createParams($data) {
          $url = array();
          foreach($data as $k=>$v) {
               $url[] = $k.'='.urlencode($v);
          }
          $url = implode('&',$url);
          return $url;
     }
     
  	function get($url) {
		$this->request($url);
	}
  
  	function post() {
  	}
}
?>